<?php
namespace haibao\jiepai\data;

use \haibao\frame\data\MysqlData;

abstract class BaseMysql extends MysqlData
{
    private static $cacheObject = null;

    public function __construct($modelName)
    {
        parent::__construct($modelName);
        $this->setConfig(\haibao\classlibrary\jiepai\Config::CLASSLIBRARY_CONFIG_MYSQL_STREET_SNAP_DB);
        $this->query('set charset utf8mb4');
    }

    /**
     * @param $dataArr
     * @param string $mapKey
     * @param bool $isExplode 是否是直接以逗号分割的
     * @return array
     */
    public static function mergeDataAndImage($dataArr, $mapKey = 'ContentInfo', $isExplode = false)
    {
        if (!$dataArr) return array();
        if (!$isExplode) {
            $tmpImageIds = array();
            foreach ($dataArr as $arr) {
                if (isset($arr[$mapKey])) {
                    array_push($tmpImageIds, (int)$arr[$mapKey]);
                }
            }
        } else {
            $dataArr = $tmpImageIds = explode(',', $dataArr);
        }
        $imageList = \haibao\cms\business\ImageLibrary::getImagesById($tmpImageIds);
        $tmpImageList = array();
        foreach ($imageList as $image) {
            $image = $image->toArray();
            $tmpImageList[$image['Id']] = $image;
        }

        if (!$isExplode) {
            foreach ($dataArr as $key => $data) {
                $dataArr[$key] = array_merge($dataArr[$key], self::initImageInfo());
                if (isset($data[$mapKey]) && isset($tmpImageList[$data[$mapKey]])) {
                    $dataArr[$key]['Picture'] = $tmpImageList[$data[$mapKey]]['Filename'];
                    $dataArr[$key]['FileWidth'] = $dataArr[$key]['CropWidth'] = $tmpImageList[$data[$mapKey]]['Width'];
                    $dataArr[$key]['FileHeight'] = $dataArr[$key]['CropHeight'] = $tmpImageList[$data[$mapKey]]['Height'];
                    $dataArr[$key]['Size'] = $tmpImageList[$data[$mapKey]]['Size'];
                    $dataArr[$key]['FileDescription'] = $tmpImageList[$data[$mapKey]]['Description'];
                    $dataArr[$key]['Ratio'] = self::imageRatio($tmpImageList[$data[$mapKey]]['Width'], $tmpImageList[$data[$mapKey]]['Height']);
                }
            }
        } else {
            foreach ($dataArr as $key => $imageId) {
                unset($dataArr[$key]);
                if (!isset($tmpImageList[$imageId])) continue;
                $dataArr[$imageId]['Picture'] = $tmpImageList[$imageId]['Filename'];
                $dataArr[$imageId]['FileWidth'] = $tmpImageList[$imageId]['Width'];
                $dataArr[$imageId]['FileHeight'] = $tmpImageList[$imageId]['Height'];
                $dataArr[$imageId]['CropWidth'] = $tmpImageList[$imageId]['Width'];
                $dataArr[$imageId]['CropHeight'] = $tmpImageList[$imageId]['Height'];
                $dataArr[$imageId]['Size'] = $tmpImageList[$imageId]['Size'];
                $dataArr[$imageId]['FileDescription'] = $tmpImageList[$imageId]['Description'];
                $dataArr[$imageId]['Ratio'] = self::imageRatio($tmpImageList[$imageId]['Width'], $tmpImageList[$imageId]['Height']);
            }
        }
        return $dataArr;
    }

    public static function mergeOneDataAndImage($data, $imageId)
    {

        if(!$imageId) return array_merge($data, self::initImageInfo());
        $imageInfo = \haibao\cms\business\ImageLibrary::getImageById($imageId);

        $data = array_merge($data, self::initImageInfo());
        if ($imageInfo) {
            $data['Picture'] = $imageInfo->Filename;
            $data['FileWidth'] = $data['CropWidth'] = $imageInfo->Width;
            $data['FileHeight'] = $data['CropHeight'] = $imageInfo->Height;
            $data['Ratio'] = self::imageRatio($imageInfo->Width, $imageInfo->Height);
        }
        return $data;
    }

    public static function initImageInfo()
    {
        return array('Picture' => '', 'FileWidth' => 0, 'FileHeight' => 0, 'CropX' => 0, 'CropY' => 0, 'CropWidth' => 0, 'CropHeight' => 0, 'Ratio' => 0);
    }

    public function normalQuery($sql, $queryData = array())
    {
        $list = array();
        if ($queryData) {
            $result = $this->query($sql, $queryData);
        } else {
            $result = $this->query($sql);
        }
        while ($row = $result->fetch_assoc()) {
            array_push($list, $row);
        }
        return $list;
    }

    public function getCmsDataMap($appId, $dataType, $isAppId = true){

        if($isAppId){
            $sql = "select CmsDataID as mapId from cms_data_map where AppDataID=%s and DataType=%s";
        }else{
            $sql = "select AppDataID as mapId  from cms_data_map where CmsDataID=%s and DataType=%s";
        }
        return $this->query($sql, array($appId, $dataType))->fetch_assoc();
    }

    /**
     * 计算图片宽高比例 1->2:3,2->1:1,3->3:2
     * @param int $width 图片宽度
     * @param int $height 图片高度
     */
    public static function imageRatio($width = 0, $height = 0)
    {
        if (!$width || !$height) {
            $ratio = 2;
        }else{
            // 按原尺寸
            $value = $width / $height;
            if ($value < 0.83) {
                $ratio = 1;
            } elseif ($value >= 0.83 && $value <= 1.25) {
                $ratio = 2;
            } elseif ($value > 1.25) {
                $ratio = 3;
            }
        }

        return $ratio;
    }
}